package com.itcat.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.PmsProductApi;
import com.itcat.mapper.PmsProductMapper;
import com.itcat.mysql.param.PmsProductParam;
import com.itcat.mysql.pojo.PmsProduct;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author Jerry Broker
 * @project_name rainbow-mall
 * @create 2022-04-02 16:22
 */
@DubboService
public class PmsProductApiImpl implements PmsProductApi {
    @Autowired
    private PmsProductMapper pmsProductMapper;

    /**
     * 分页查询商品信息
     * @param pmsProductParam
     * @return
     */
    @Override
    public Page<PmsProduct> list(PmsProductParam pmsProductParam) {

        // 构建分页对象
        Page<PmsProduct> page = new Page<>(pmsProductParam.getPageNum(), pmsProductParam.getPageSize());

        // 构建查询条件
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(pmsProductParam.getBrandId() != null, PmsProduct::getBrandId, pmsProductParam.getBrandId());
        queryWrapper.like(pmsProductParam.getKeyword() != null, PmsProduct::getName, pmsProductParam.getKeyword());
        queryWrapper.eq(pmsProductParam.getProductCategoryId() != null, PmsProduct::getProductCategoryId, pmsProductParam.getProductCategoryId());
        queryWrapper.eq(pmsProductParam.getPublishStatus() != null, PmsProduct::getPublishStatus, pmsProductParam.getPublishStatus());
        queryWrapper.eq(pmsProductParam.getVerifyStatus() != null, PmsProduct::getVerifyStatus, pmsProductParam.getVerifyStatus());
        queryWrapper.eq(pmsProductParam.getProductSn()!= null, PmsProduct::getProductSn, pmsProductParam.getProductSn());
        queryWrapper.ne(PmsProduct::getDeleteStatus, 1);
        queryWrapper.orderByDesc(PmsProduct::getSort);

        // 查询结果
        pmsProductMapper.selectPage(page, queryWrapper);


        return page;
    }

    /**
     * 保存商品信息
     * @param pmsProduct
     * @return
     */
    @Override
    public Long save(PmsProduct pmsProduct) {
        int insert = pmsProductMapper.insert(pmsProduct);
        return pmsProduct.getId();
    }

    /**
     * 更改删除状态
     * @param list
     * @param deleteStatus
     */
    @Override
    public void updateDeleteStatus(List<Integer> list, Integer deleteStatus) {
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setDeleteStatus(deleteStatus);
        LambdaUpdateWrapper<PmsProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(deleteStatus != null , PmsProduct::getDeleteStatus, deleteStatus).in(PmsProduct::getId, list);
        pmsProductMapper.update(pmsProduct, updateWrapper);
    }

    /**
     * 批量设为新品
     * @param list
     * @param newStatus
     */
    @Override
    public void updateNewStatus(List<Integer> list, Integer newStatus) {
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setNewStatus(newStatus);
        LambdaUpdateWrapper<PmsProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(newStatus != null , PmsProduct::getNewStatus, newStatus).in(PmsProduct::getId, list);
        pmsProductMapper.update(pmsProduct, updateWrapper);
    }

    /**
     * 批量上下架商品
     * @param list
     * @param publishStatus
     */
    @Override
    public void updatePublishStatus(List<Integer> list, Integer publishStatus) {
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setPublishStatus(publishStatus);
        LambdaUpdateWrapper<PmsProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(publishStatus != null , PmsProduct::getPublishStatus, publishStatus).in(PmsProduct::getId, list);
        pmsProductMapper.update(pmsProduct, updateWrapper);
    }

    /**
     * 批量推荐商品
     * @param list
     * @param recommendStatus
     */
    @Override
    public void updateRecommendStatus(List<Integer> list, Integer recommendStatus) {
        PmsProduct pmsProduct = new PmsProduct();
        pmsProduct.setRecommandStatus(recommendStatus);
        LambdaUpdateWrapper<PmsProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(recommendStatus != null , PmsProduct::getRecommandStatus, recommendStatus).in(PmsProduct::getId, list);
        pmsProductMapper.update(pmsProduct, updateWrapper);
    }

    /**
     * 根据商品id获取商品编辑信息
     * @param id
     * @return
     */
    @Override
    public PmsProduct findById(Integer id) {
        PmsProduct pmsProduct = pmsProductMapper.selectById(id);
        return pmsProduct;
    }


    /**
     * 更新商品基本信息
     * @param pmsProduct
     */
    @Override
    public void update(PmsProduct pmsProduct) {
        int i = pmsProductMapper.updateById(pmsProduct);
    }
}
